$(function () {
    initLayUI();
});

function initLayUI() {
    layui.use(['layer'], function () {

        $("#login").click(function () {
            login();
        });

        $("body").keydown(function (event) {
            if (event.keyCode == '13') {
                login();
            }
        });
    });
}

function login() {
    let phone = $("#phone").val();
    let password = $("#password").val();
    let loadingIndex = layerUtil.openLoading(1);
    ajaxUtil.post({
        phone: phone,
        password: password
    }, "/login", function (res) {
        if (res.code === 200) {
            tokenUtil.setToken(res.data.token);
            putTempData('userInfo', res.data);
            layerUtil.closeLayer(loadingIndex);
            layerUtil.openMsg({
                icon: 1,
                msg: '登录成功',
                time: 1000
            }, function () {
                location.href = "index.html";
            });
        } else {
            layerUtil.closeLayer(loadingIndex);
            layerUtil.openMsgError(res.msg);
        }
    }, function () {
        layerUtil.closeLayer(loadingIndex);
        layerUtil.openMsgError("登录失败，请稍后再试");
    });
}